// Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
// Distributable under LGPL license. See terms of license at gnu.org.

package nl.justobjects.pushlet.test;

import java.net.URL;

import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.client.JavaPushletClient;
import nl.justobjects.pushlet.client.JavaPushletClientListener;

/**
 * Tester to demonstrate Pushlet/Postlet use in Java applications.
 *
 * This program does two things:
 * (1) it subscribes to the subject "test/ping" with a Pushlet and
 * (2) it pushes an Event with subject "/test/ping" with a Postlet every
 * few seconds.
 *
 * @version $Id: PushletPingApplication.java,v 1.4 2003/08/17 20:30:20 justb Exp $
 * @author Just van den Broecke - Just Objects &copy;
 **/
public class PushletPingApplication implements JavaPushletClientListener {
	private String pushletUrl = null;
	private String postletUrl = null;
	private JavaPushletClient javaPushletClient;

	public PushletPingApplication(String host) {
		// Set the URLs for Pushlet and Postlet with the supplied host:port
		pushletUrl = "http://" + host + "/pushlet/servlet/pushlet?format=xml&subject=/test/ping";
		postletUrl = "http://" + host + "/pushlet/servlet/postlet?subject=/test/ping";
		p("pushletUrl=" + pushletUrl);
		p("postletUrl=" + postletUrl);

		// Create and start a Pushlet client; we receive callbacks
		// through probe() and event(). JavaPushletClient runs in its
		// own Thread.
		javaPushletClient = new JavaPushletClient();
		javaPushletClient.start(this, pushletUrl);
		p("javaPushletClient started");
		int seqNr = 1;

		// Post an event to the server every 5 seconds.
		while (true) {
			try {
				p("posting ping...");

				// Just creating the URL, opening the connection and getting
				// the input stream is enough to issue the GET HTTP request.
				new URL(postletUrl + "&seqNr=" + (seqNr++)).openConnection().getInputStream();
				p("posted ping - sleeping...");
				Thread.sleep(5000);
			} catch (Exception e) {
				p("Postlet exception: " + e);
				System.exit(-1);
			}
		}
	}

	/** Server has pushed probe event. */
	public void onProbe() {
		p("received: <probe/>");
	}

	/** Server has pushed event. */
	public void onEvent(Event theEvent) {
		p("received: " + theEvent.toXML());
	}

	/** Error occurred. */
	public void onError(String message) {
		p(message);
	}

	/** Generic print. */
	public void p(String s) {
		System.out.println("PushletPingApplication: " + s);
	}

	/** Main program. */
	public static void main(String args[]) {
		if (args.length == 0) {
			new PushletPingApplication("lombok:8080");
		} else {
			// Supply a host:port string
			new PushletPingApplication(args[0]);
		}

	}
}


/*
 * $Log: PushletPingApplication.java,v $
 * Revision 1.4  2003/08/17 20:30:20  justb
 * cosmetic changes
 *
 * Revision 1.3  2003/08/15 08:37:41  justb
 * fix/add Copyright+LGPL file headers and footers
 *
 * Revision 1.2  2003/05/18 16:15:08  justb
 * support for XML encoded Events
 *
 * Revision 1.1.1.1  2002/09/24 21:02:33  justb
 * import to sourceforge
 *
 * Revision 1.1.1.1  2002/09/20 22:48:19  justb
 * import to SF
 *
 * Revision 1.1.1.1  2002/09/20 14:19:02  justb
 * first import into SF
 *
 * Revision 1.3  2000/08/31 12:49:50  just
 * added CVS comment tags for log and copyright
 *
 *
 */
